package com.gtis.demo;

import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
@Service
public class MyProxyService implements InvocationHandler {


    private Object target ;//需要代理的目标对象
    private static final Map map=new HashMap();;
    public MyProxyService(){
        map.put("shiqu","hoOneService");
        map.put("jiaoqu","hoTwoService");
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        return result;
    }
    //定义获取代理对象方法
    public Object getJDKProxy(String dq){
        String beanId="qlztService";
        if(dq!=null && map.get(dq)!=null){
            //获取地区转换beanid
            beanId = map.get(dq).toString();

        }
        Object targetObject=ApplicationContextHelper.getBean(beanId);
        //为目标对象target赋值
        this.target =targetObject  ;
        //JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance 函数所需参数就可看出
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this);
    }
  /*  @Resource
    private QlztProxyService qlztProxyService;
    public static void main(String[] args) {


        QlztService qlztService = (QlztService) qlztProxyService.getJDKProxy("");//获取代理对象
        //qlztService.addUser("admin", "123123");//执行新增方法
    }*/

}
